﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
   public class Create
    {
       //创建模型对象
       public static int CreateModel(string Name, string AdminDir, string WebDir, string TableName, string Remark)
       {
           #region 检查输入的合法性
           if (Name == "") return 0;
           if (AdminDir == "") return 0;
           if (WebDir == "") return 0;
           if (TableName == "") return 0;
           #endregion

           #region 把数据组装成一个对象
           Models.Model model = new Models.Model();
           model.Name = Name;
           model.AdminDir = AdminDir;
           model.WebDir = WebDir;
           model.TableName = TableName;
           model.Remark = Remark;
           #endregion

           return DAL.Create.CreateModel(model);
       }
       //添加日志
       public static int CreateLogRecord(string UserName, string UserIP, string Action)
       {
           #region 把数据组装成一个对象
           Models.Log log = new Models.Log();
           log.UserName = UserName;
           log.UserIP = UserIP;
           log.Time = DateTime.Now;
           log.Action = Action;
           #endregion

           return DAL.Create.CreateLogRecord(log);
       }
       // 添加用户对象
       public static int CreateUser(string Name, string Password, string RoleID, string UserEnabled)
       {
           #region 检查输入的合法性
           if (Name == "") return 0;
           if (Password == "") return 0;
           if (RoleID == "") return 0;
           if (UserEnabled == "") return 0;
           #endregion

           #region 把数据组装成一个对象
           Models.User user = new Models.User();
           user.Name = Name;
           user.Password = Utility.Tool.SHA256Encrypt(Password);
           user.RoleID = Convert.ToInt32(RoleID);
           user.UserEnabled = Convert.ToBoolean(UserEnabled);
           #endregion

           return DAL.Create.CreateUser(user);
       }
       // 添加角色对象
       public static int CreateRole(string Name, string RoleInfoModelID, string RoleEnabled,string IsAdmin, string ModelAdmin, string UserAdmin, string RoleAdmin, string LogAdmin, string SystemAdmin, string ColumnAdmin, string FileAdmin, string ContentAdmin)
       {
           #region 把数据组装成一个对象
           Models.Role role = new Models.Role();
           role.Name = Name;
           role.RoleInfoModelID = Convert.ToInt32(RoleInfoModelID);
           role.IsAdmin = Convert.ToBoolean(IsAdmin);
           role.RoleEnabled = Convert.ToBoolean(RoleEnabled);
          

           role.ModelAdmin = Convert.ToBoolean(ModelAdmin);      
           role.UserAdmin = Convert.ToBoolean(UserAdmin);
           role.RoleAdmin = Convert.ToBoolean(RoleAdmin);        
           role.LogAdmin = Convert.ToBoolean(LogAdmin);

           role.SystemAdmin = Convert.ToBoolean(SystemAdmin);
           role.ColumnAdmin = Convert.ToBoolean(ColumnAdmin);
           role.FileAdmin = Convert.ToBoolean(FileAdmin);
           role.ContentAdmin = ContentAdmin;

           #endregion

           return DAL.Create.CreateRole(role);
       }
       // 插入新栏目，传入栏目句，父栏目id，模型id和备注
       public static int InsertColumn(string Name, string ParentID, string ModelID,string ColumnLogo,string Remark)
       {
           #region 输入合法性检查
           if (Name == "") return 0;
           int PID, MID;
           try
           {
               PID = Convert.ToInt32(ParentID);
               MID = Convert.ToInt32(ModelID);
           }
           catch
           {
               return 0;
           }
           #endregion

           #region 把输入组装成类的实例
           Models.Column column = new Models.Column();
           column.Name = Name;
           column.ParentID = PID;
           column.ModelID = MID;
           column.ColumnLogo = ColumnLogo;
           column.Remark = Remark;
           #endregion

           return DAL.Create.InsertColumn(column);
       }
       // 插入文章模型的内容
       public static int InsertArticle(string ColumnID, string FirstEditor, string LastEditor, string Title, string Content, string UpdateDate, string Source, string Status)
       {
           #region 输入合法性检查
           if (Title == "") return 0;
           if (Content == "") return 0;
           if (Source == "") return 0;

           int CID;
           DateTime DATE;
           bool STATUS;

           try
           {
               CID = Convert.ToInt32(ColumnID);
               DATE = Convert.ToDateTime(UpdateDate);
               STATUS = Convert.ToBoolean(Status);
           }
           catch
           {
               return 0;
           }
           #endregion

           #region 把输入组装成类的实例
           Models.ModelArticle article = new Models.ModelArticle();
           article.Title = Title;
           article.ColumnID = CID;
           article.UpdateDate = DATE;
           article.Content = Content;
           article.FirstEditor = FirstEditor;
           article.LastEditor = LastEditor;
           article.Status = STATUS;
           article.Source = Source;
           #endregion

           return DAL.Create.InsertArticle(article);
       }
       // 插入单页模型的内容
       public static int InsertContent(string ColumnID, string FirstEditor, string LastEditor, string Title, string Content, string UpdateDate)
       {
           #region 输入合法性检查
           if (Title == "") return 0;
           if (Content == "") return 0;

           int CID;
           DateTime DATE;

           try
           {
               CID = Convert.ToInt32(ColumnID);
               DATE = Convert.ToDateTime(UpdateDate);
           }
           catch
           {
               return 0;
           }
           #endregion

           #region 把输入组装成类的实例
           Models.ModelContent content = new Models.ModelContent();
           content.Title = Title;
           content.ColumnID = CID;
           content.UpdateDate = DATE;
           content.Content = Content;
           content.FirstEditor = FirstEditor;
           content.LastEditor = LastEditor;
           #endregion

           return DAL.Create.InsertContent(content);
       }
       // 插入图片轮播模型的内容
       public static int InsertSlider(string ColumnID, string FirstEditor, string LastEditor, string Title, string UpdateDate, string PictureAddress, string ArticleAddress, string Remark)
       {
           #region 输入合法性检查
           if (Title == "") return 0;
           if (PictureAddress == "") return 0;       

           int CID;
           DateTime DATE;


           try
           {
               CID = Convert.ToInt32(ColumnID);
               DATE = Convert.ToDateTime(UpdateDate);
           }
           catch
           {
               return 0;
           }
           #endregion

           #region 把输入组装成类的实例
           Models.ModelSlider slider = new Models.ModelSlider();
           slider.Title = Title;
           slider.ColumnID = CID;
           slider.UpdateDate = DATE;
           slider.PictureAddress = PictureAddress;
           slider.ArticleAddress = ArticleAddress;
           slider.FirstEditor = FirstEditor;
           slider.LastEditor = LastEditor;      
           slider.Remark = Remark;
           #endregion

           return DAL.Create.InsertSlider(slider);
       }
       // 插入网站外链模型的内容
       public static int InsertLink(string ColumnID, string FirstEditor, string LastEditor, string WebUrl, string WebName, string WebLogo, string UpdateDate)
       {
           #region 输入合法性检查
           if (WebUrl == "") return 0;
           if (WebName == "") return 0;

           int CID;
           DateTime DATE;


           try
           {
               CID = Convert.ToInt32(ColumnID);
               DATE = Convert.ToDateTime(UpdateDate);
           }
           catch
           {
               return 0;
           }
           #endregion

           #region 把输入组装成类的实例
           Models.ModelLink link = new Models.ModelLink();
           link.WebUrl = WebUrl;
           link.ColumnID = CID;
           link.UpdateDate = DATE;
           link.WebName = WebName;
           link.WebLogo = WebLogo;
           link.FirstEditor = FirstEditor;
           link.LastEditor = LastEditor;
 
           #endregion

           return DAL.Create.InsertLink(link);
       }
       // 插入办公文件模型的内容
       public static int InsertOfficeFile(string ColumnID, string FileName, string FileEditor, string FileType, string FileSize, string FileUrl, string UploadTime,string UploadSource)
       {
           #region 输入合法性检查
           if (FileName == "") return 0;
           if (FileUrl == "") return 0;

           int CID;
           DateTime DATE;


           try
           {
               CID = Convert.ToInt32(ColumnID);
               DATE = Convert.ToDateTime(UploadTime);
           }
           catch
           {
               return 0;
           }
           #endregion

           #region 把输入组装成类的实例
           Models.ModelOfficeFile File = new Models.ModelOfficeFile();
           File.ColumnID = CID;
           File.FileName = FileName;
           File.FileEditor = FileEditor;
           File.FileType = FileType;
           File.FileSize = FileSize;
           File.FileUrl = FileUrl;
           File.UploadTime = DATE;
           File.UploadSource = UploadSource;

           #endregion

           return DAL.Create.InsertOfficeFile(File);
       }
    }
}
